package com.david.mydemo.framework.aspect;

import com.david.mydemo.framework.annotation.MonitorVisits;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * @ClassName MonitorVisitsAspect
 * @Description 监控访问量自定义注解
 */
@Aspect
@Component
@Slf4j
public class MonitorVisitsAspect {

    private static final String USER_KEY = "userTag";

    @Autowired
//    private ServerDetialService serverDetialService;

    @Pointcut("@annotation(com.david.mydemo.framework.annotation.MonitorVisits)")
    public void annotationPointcut() {

    }

    @Before("annotationPointcut()")
    public void beforePointcut(JoinPoint joinPoint) {
        // 此处进入到方法前  可以实现一些业务逻辑
    }

    @Around("annotationPointcut()")
    public Object doAround(ProceedingJoinPoint joinPoint) throws Throwable {
//        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
//        String[] params = methodSignature.getParameterNames(); // 获取参数名称
//        Object[] args = joinPoint.getArgs(); // 获取参数值
//        Class[] paramTypes = methodSignature.getParameterTypes(); // 获取参数类型

        return joinPoint.proceed();
    }

    @AfterReturning(value = "annotationPointcut()", returning = "result")
    public void doAfterReturning(JoinPoint joinPoint, Object result) {
        MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
        MonitorVisits annotation = methodSignature.getMethod().getAnnotation(MonitorVisits.class);
        String[] params = methodSignature.getParameterNames(); // 获取参数名称
        Object[] args = joinPoint.getArgs(); // 获取参数值

        try {
            // 各类图层服务访问数量统计批量插入
//            serverDetialService.insertBatch(list);
        } catch (Exception e) {
            log.error("切面统计图层服务访问情况出错，异常信息：", e);
        }

    }


}

